<?php


class editGroup extends bee\form
{ protected static $__formID = "frm201202022138";
  public $__caption = "Edit group";
  public $__version = "1.0";
  protected $__activeMenu = "groupOverview";

  protected $group;
  protected $error;
  protected $tId;
  protected $tName;
  protected $tDescription;

  public function createForm($evt)
  { $this->group = new bee\group();
    $this->error = new bee\serror();
    if(isset($evt["id"]))
    { $this->group->SELECT($evt["id"]);
    }
    $this->tId = new bee\text("id", $this->group->id);
    $this->tName = new bee\text("name", $this->group->name);
    $this->tDescription = new bee\text("description", $this->group->description);
  }

  public function drawForm($evt)
  { static::$DSDoc->setRow($this->group);
  }

  public function add($evt)
  { $this->group->fillFromArray($evt);
    if($this->validate($this->group, $this->error))
    { $this->group->INSERT();
      bee\app::setMessage("Group successfully added.");
    } else
    { bee\app::setMessage($this->error->err_description, MSG_LEVEL_WARNING);
    }
  }

  public function edit($evt)
  { $this->group->fillFromArray($evt);
    if($this->validate($this->group, $this->error))
    { $this->group->UPDATE();
      bee\app::setMessage("Group successfully updated.");
    } else
    { bee\app::setMessage($this->error->err_description, MSG_LEVEL_WARNING);
    }
  }

  public function delete($evt)
  {
    $gr_usr = new bee\gr_usr();
    $gr_usr->group_id = $this->group->id;
    $rs_gr_usr = new bee\rowSet(clone $gr_usr);
    if($rs_gr_usr->getRow($gr_usr))
    { bee\app::setMessage("Group \"" . $this->group->name . "\" can't be deleted becuse is in use.", MSG_LEVEL_WARNING);
    } else
    { $this->group->DELETE();
      bee\app::setMessage("Group \"" . $this->group->name . "\" successfully deleted.");
    }
    header("Location: index.php?__form=" . $this->__parent . "&sys_msg=" . urlencode(bee\app::getMessageText()) . "&sys_msg_style=" . bee\app::getMessageLevel() );
  }

  protected function validate(bee\group $row, bee\serror &$error)
  { if(!$row->name)
    { $error->err_code = -10;
      $error->err_description .= "\nName must be filled!";
      return false;
    }
    $error->err_code = 0;
    return true;
  }
}